<?php

//Inclure la class d'accès à la base de données
include_once '../../DBAccess/DBAccess.php';

class AlgoWindows {

    public $ti; //Température intérieure
    public $te; //Température extérieure
    public $textra; //Extrapolation - supplément de régulation
    public $tc = 365; //Durée de la période de calcul
    public $totalSurfaceNorth; // Surface totales de fenêtres au Nord
    public $coefNorthArray = array(); //Coeficient des fenêtres au Nord
    public $totalCoefNorth; //Moyenne des coeficients des fenêtre Nord
    public $totalSurfaceSouth; // Surface totales de fenêtres au Sud
    public $coefSouthArray = array(); //Coeficient des fenêtres au Sud
    public $totalCoefSouth; //Moyenne des coeficients des fenêtre Sud
    public $totalSurfaceEast; // Surface totales de fenêtres au Est
    public $coefEastArray = array(); //Coeficient des fenêtres au Est
    public $totalCoefEast; //Moyenne des coeficients des fenêtre Est
    public $totalSurfaceWest; // Surface totales de fenêtres au Ouest
    public $coefWestArray = array(); //Coeficient des fenêtres au Ouest
    public $totalCoefWest; //Moyenne des coeficients des fenêtre Ouest
    public $Uff; //Valeur U d'une fenêtre
    public $deperditionNorth; //Déperdition pour les fenêtres au Nord
    public $deperditionEast; //Déperdition pour les fenêtres a l Est
    public $deperditionWest; //Déperdition pour les fenêtres a l Ouest
    public $deperditionSouth; //Déperdition pour les fenêtres au Sud
    public $totalDeperdition; // Déperdition totale de toutes les fenêtres

    //Fonction qui calcul la déperdition totales de toutes les fenêtres 

    function getTotalDepertitionWindows($house) {
        //parcour de tous les murs
        foreach ($house->walls as $value) {
            //parcour de toutes les fenêtres
            foreach ($value->windows as $window) {
                //calcul selon l'orientation
                switch ($value->orientation) {
                    //Orientation north
                    case 1:
                        $this->totalSurfaceNorth += $this->getSurface($window);
                        array_push($this->coefNorthArray, $this->getUwf($window, $house->year));
                        break;
                    //Orientation south
                    case 3:
                        $this->totalSurfaceSouth += $this->getSurface($window);
                        array_push($this->coefSouthArray, $this->getUwf($window, $house->year));
                        break;
                    //Orientation east
                    case 2:
                        $this->totalSurfaceEast += $this->getSurface($window);
                        array_push($this->coefEastArray, $this->getUwf($window, $house->year));
                        break;
                    //Orientation west
                    case 4:
                        $this->totalSurfaceWest += $this->getSurface($window);
                        array_push($this->coefWestArray, $this->getUwf($window, $house->year));
                        break;

                    default:
                        echo "Orientation du mur non connue....";
                        break;
                }
            }
        }

        //Récupération de la température extérieure selon la ville de résidence
        $this->te = DBAccess::getExTemp($house->idCity);
         //Récupération du supplément de régulation pour la température ambiante
        $this->textra = DBAccess::getTempExtrapolation($house->year);
        //Température exemple
        $this->ti = $house->roomsTemp;
        $this->ti = $this->ti + $this->textra;
        

        //Moyenne des coéficients pour chaque orientation
        if (!empty($this->coefNorthArray)) {
            $this->totalCoefNorth = array_sum($this->coefNorthArray) / count($this->coefNorthArray);
        }

        if (!empty($this->coefSouthArray)) {
            $this->totalCoefSouth = array_sum($this->coefSouthArray) / count($this->coefSouthArray);
        }

        if (!empty($this->coefEastArray)) {
            $this->totalCoefEast = array_sum($this->coefEastArray) / count($this->coefEastArray);
        }

        if (!empty($this->coefWestArray)) {
            $this->totalCoefWest = array_sum($this->coefWestArray) / count($this->coefWestArray);
        }
             
        $this->deperditionNorth = ($house->roomsTemp - $this->te) * $this->tc * $this->totalSurfaceNorth * $this->totalCoefNorth * 86400 / (1000000 * $this->getSRE($house));
        $this->deperditionEast = ($house->roomsTemp - $this->te) * $this->tc * $this->totalSurfaceEast * $this->totalCoefEast * 86400 / (1000000 * $this->getSRE($house));
        $this->deperditionWest = ($house->roomsTemp - $this->te) * $this->tc * $this->totalSurfaceWest * $this->totalCoefWest * 86400 / (1000000 * $this->getSRE($house));
        $this->deperditionSouth = ($house->roomsTemp - $this->te) * $this->tc * $this->totalSurfaceSouth * $this->totalCoefSouth * 86400 / (1000000 * $this->getSRE($house));
        
        //Addition de toutes les déperditions pour le total
        $this->totalDeperdition = $this->deperditionNorth + $this->deperditionEast + $this->deperditionSouth + $this->deperditionWest;

        return $this->totalDeperdition;
    }

    //Retourne la surface d'une fenêtre
    function getSurface($window) {
        return $window->l1 * $window->l2;
    }

    function getUwf($window, $year) {

        $Uff = $this->getUff($window);
        $Ugf = $this->getUgf($window);

        if ($window->hasSunBlind) {
            $Ustf = $this->getUstf($year);
        } else {
            $Ustf = 0;
        }

        $Psif = 0.07833333;

        return (($Uff * $window->A5) + ($Ugf * $window->A3) + ($Ustf * $window->A2) + ($Psif * $window->LT4)) / $window->A1;
    }

    function getUff($window) {
        return DBAccess::getValueU_Frame($window->idFrame);
    }

    function getUgf($window) {
        return DBAccess::getValueU_Glass($window->idGlass);
    }

    function getUstf($year) {
        return DBAccess::getValueU_Sunblind($year);
    }

    function getSRE($house) {
        switch ($house->idShape) {
            // Forme carré et rectangle
            case 1:
                $this->totalSurface = $house->M1 * $house->M1;
                $this->totalSurface *= $house->floors;

                break;
            case 2:
                $this->totalSurface = $house->M1 * $house->M2;
                $this->totalSurface *= $house->floors;

                break;
            //Forme en L
            case 3:
                $this->totalSurface = ($house->M1 * $house->M4) + ($house->M2 - $house->M4) * $house->M3;
                $this->totalSurface *= $house->floors;
                break;
            //Forme en U
            case 4:
                $this->totalSurface = ($house->M1 * $house->M6) + ($house->M4 * $house->M5) + ($house->M2 - $house->M6) * $house->M3;
                $this->totalSurface *= $house->floors;
                break;
            //Forme rect. décalé
            case 5:
                $this->totalSurface = ($house->M1 * $house->M2) + $house->M6 * ($house->M7 - $house->M4) + ($house->M4 * $house->M5);
                $this->totalSurface *= $house->floors;
                break;
            //Forme en T
            case 6:
                $this->totalSurface = ($house->M1 * $house->M2) + ($house->M4 * $house->M5);
                $this->totalSurface *= $house->floors;
                break;

            default :
                echo 'Forme de la maison non connue....';
                break;
        }
        return $this->totalSurface;
    }

}
?>